package com.clock.shop.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.clock.common.enums.shop.OrderPayStatus;
import com.clock.common.enums.shop.OrderPaymentType;
import com.clock.common.enums.shop.OrderStatus;
import com.clock.common.utils.DateUtils;
import com.clock.shop.domain.ShopOrder;
import com.clock.shop.domain.vo.ShopOrderVo;
import com.clock.shop.mapper.ShopOrderMapper;
import com.clock.shop.service.IShopOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-12
 */
@Service
public class ShopOrderServiceImpl implements IShopOrderService
{
    @Resource
    private ShopOrderMapper shopOrderMapper;

    @Autowired
    private ShopShopServiceImpl shopShopService;

    //查本月金额
    public BigDecimal selectMonthMoney(Long shopId) {
        return shopOrderMapper.selectMonthMoney(shopId);
    }

    //查本月订单数
    public Long selectMonthOrder(Long shopId) {
        return shopOrderMapper.selectMonthOrder(shopId);
    }

    //查今日金额
    public BigDecimal selectTodayMoney(Long shopId) {
        return shopOrderMapper.selectTodayMoney(shopId);
    }

    //查今日订单数
    public Long selectTodayOrder(Long shopId) {
        return shopOrderMapper.selectTodayOrder(shopId);
    }

    //查询订单总金额
    public BigDecimal selectTotalMoney(Long shopId)
    {
        return shopOrderMapper.selectTotalMoney(shopId);
    }
    //查询订单总量
    @Override
    public Long selectOrderNum(Long shopId)
    {
        return shopOrderMapper.selectOrderNum(shopId);
    }
    /**
     * 查询订单
     *
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public ShopOrder selectShopOrderByOrderId(Long orderId)
    {
        return shopOrderMapper.selectShopOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     *
     * @param orderVo 订单
     * @return 订单
     */
    @Override
    public List<ShopOrderVo> selectShopOrderList(ShopOrderVo orderVo)
    {
        List<ShopOrderVo> list = shopOrderMapper.selectShopOrderList(orderVo);
        for (ShopOrderVo shopOrderVo : list) {
            // 设置订单支付状态
            if(shopOrderVo.getPayStatus().equals(OrderPayStatus.UNPAID.getCode())){
                shopOrderVo.setPayStatus(OrderPayStatus.UNPAID.getInfo());
            }else{
                shopOrderVo.setPayStatus(OrderPayStatus.PAID.getInfo());
                // 设置订单付款类型
                if(shopOrderVo.getPaymentType().equals(OrderPaymentType.ONLINEPAYMENT.getCode())){
                    shopOrderVo.setPaymentType(OrderPaymentType.ONLINEPAYMENT.getInfo());
                }else {
                    shopOrderVo.setPaymentType(OrderPaymentType.CASH.getInfo());
                }
            }
        }
        return list;
    }

    /**
     * 新增订单
     *
     * @param shopOrderVo 订单
     * @return 结果
     */
    @Override
    public int insertShopOrder(ShopOrderVo shopOrderVo)
    {
        // ShopOrder shopOrder = new ShopOrder();
        // 根据用户名查询用户是否存在，存在则返回用户ID
        // Long userId = sysUserService.selectUserByUserName(shopOrderVo.getUserName()).getUserId();
        // if(userId != null){
        //     shopOrder.setUserId(userId);
        // }else {
        //     return 0;
        // }
        // // 根据店铺名查询店铺是否存在，存在则返回店铺ID
        // Long shopId = shopShopService.selectShopShopByShopName(shopOrderVo.getShopName()).get(0).getShopId();
        // if(shopId != null){
        //     shopOrder.setShopId(shopId);
        // }else {
        //     return 0;
        // }
        shopOrderVo.setCreateTime(DateUtils.getNowDate());
        // 判断订单支付状态
        // 已支付，设置支付类型，支付时间,订单状态
        if(shopOrderVo.getPayStatus().equals(OrderPayStatus.PAID.getCode())){
            shopOrderVo.setPayStatus(OrderPayStatus.PAID.getCode());
            if(shopOrderVo.getPaymentType().equals(OrderPaymentType.ONLINEPAYMENT.getCode())){
                shopOrderVo.setPaymentType(OrderPaymentType.ONLINEPAYMENT.getCode());
            }else {
                shopOrderVo.setPaymentType(OrderPaymentType.CASH.getCode());
            }
            shopOrderVo.setOrderStatus(OrderStatus.UNUSED.getInfo());
            shopOrderVo.setPayTime(DateUtils.getNowDate());
        }else {
            shopOrderVo.setPayStatus(OrderPayStatus.UNPAID.getCode());
        }

        // 设置更新时间
        shopOrderVo.setUpdateTime(DateUtils.getNowDate());
        return shopOrderMapper.insertShopOrder(shopOrderVo);
    }
    @Override
    public int takeShopOrder(ShopOrder shopOrder){
        shopOrder.setCreateTime(DateUtils.getNowDate());
        // 判断订单支付状态
        // 已支付，设置支付类型，支付时间,订单状态
        if(shopOrder.getPayStatus().equals(OrderPayStatus.PAID.getCode())){
            shopOrder.setPayStatus(OrderPayStatus.PAID.getCode());
            if(shopOrder.getPaymentType().equals(OrderPaymentType.ONLINEPAYMENT.getCode())){
                shopOrder.setPaymentType(OrderPaymentType.ONLINEPAYMENT.getCode());
            }else {
                shopOrder.setPaymentType(OrderPaymentType.CASH.getCode());
            }
            shopOrder.setOrderStatus(OrderStatus.UNUSED.getInfo());
            shopOrder.setPayTime(DateUtils.getNowDate());
        }else {
            shopOrder.setPayStatus(OrderPayStatus.UNPAID.getCode());
        }

        // 设置更新时间
        shopOrder.setUpdateTime(DateUtils.getNowDate());
        return shopOrderMapper.takeShopOrder(shopOrder);
    }

    /**
     * 修改订单
     *
     * @param shopOrder 订单
     * @return 结果
     */
    @Override
    public int updateShopOrder(ShopOrder shopOrder)
    {
        shopOrder.setUpdateTime(DateUtils.getNowDate());
        return shopOrderMapper.updateShopOrder(shopOrder);
    }

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteShopOrderByOrderIds(Long[] orderIds)
    {
        return shopOrderMapper.deleteShopOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     *
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteShopOrderByOrderId(Long orderId)
    {
        return shopOrderMapper.deleteShopOrderByOrderId(orderId);
    }
}
